package cn.rokhdelar.umbrsapi.core.jpa;



import cn.rokhdelar.umbrsapi.core.utils.EscapeUtil;
import org.hibernate.type.BasicTypeReference;
import org.hibernate.type.StandardBasicTypes;
import org.hibernate.type.Type;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;


@SuppressWarnings("serial")
public class FilterMap extends HashMap<String, Object> {
    public static final String SPLIT = "_";
	/**
	 * 等于
	 * key_eq,value
	 * @param key
	 * @param value
	 */
	public FilterMap eq(String key, Object value) {
		this.put(key + SPLIT + CriteriaConstant.EQ, value);
		return this;
	}

	/**
	 * 不等于
	 * 
	 * @param key
	 * @param value
	 */
	public FilterMap ne(String key, Object value) {
		this.put(key + SPLIT + CriteriaConstant.NE, value);
		return this;
	}

	/**
	 * 大于
	 * 
	 * @param key
	 * @param value
	 */
	public FilterMap gt(String key, Object value) {
		this.put(key + SPLIT + CriteriaConstant.GT, value);
		return this;
	}

	/**
	 * 大于等于
	 * 
	 * @param key
	 * @param value
	 */
	public FilterMap ge(String key, Object value) {
		this.put(key + SPLIT + CriteriaConstant.GE, value);
		return this;
	}

	/**
	 * 小于
	 * 
	 * @param key
	 * @param value
	 */
	public FilterMap lt(String key, Object value) {
		this.put(key + SPLIT + CriteriaConstant.LT, value);
		return this;
	}

	/**
	 * 小于等于
	 * 
	 * @param key
	 * @param value
	 */
	public FilterMap le(String key, Object value) {
		this.put(key + SPLIT + CriteriaConstant.LE, value);
		return this;
	}

	/**
	 * 之间
	 * 
	 * @param key
	 * @param map
	 */
	public FilterMap between(String key, Map<String, Object> map) {
		this.put(key + SPLIT + CriteriaConstant.BETWEEN, map);
		return this;
	}

	/**
	 * 相似
	 */
	public FilterMap likeVal(String key, String value) {
		return likeVal(key,value,true,true);
	}
	
	/**
	 * 相似
	 * 
	 * @param key
	 * @param value
	 * @param prefix 是否加前缀
	 * @param suffix 是否加后缀
	 */
	private FilterMap likeVal(String key, String value,boolean prefix,boolean suffix) {
		value= EscapeUtil.escapeChar(value);
		if(prefix){
			value="%"+value;
		}
		if(suffix){
			value=value+"%";
		}
		Object[] das= {new Object[]{value},new BasicTypeReference[]{StandardBasicTypes.STRING}};
//		this.put(SPLIT + CriteriaConstant.SQLPARAMS+SPLIT+new Random().nextInt(1000)+SPLIT+ key+" "+ CriteriaConstant.LIKE +" ? ",das);
		this.put(key + SPLIT + CriteriaConstant.LIKE, das);
		return this;
	}

	/**
	 * 或者
	 *
	 * @param key
	 * @param value
	 */
	public FilterMap or(String key, FilterMap value) {
		this.put(key + SPLIT + CriteriaConstant.OR, value);
		return this;
	}


	/**
	 * 之内
	 * 
	 * @param key
	 * @param value
	 */
	public FilterMap in(String key, Object[] value) {
		this.put(key + SPLIT + CriteriaConstant.IN, value);
		return this;
	}

	/**
	 * 之外
	 * @param key
	 * @param value
	 */
	public FilterMap notin(String key, Object[] value) {
		this.put(key + SPLIT + CriteriaConstant.NOTIN, value );
		return this;
	}

	/**
	 * 为空
	 * 
	 * @param key
	 */
	public FilterMap isNull(String key) {
		this.put(key + SPLIT + CriteriaConstant.ISNULL, null);
		return this;
	}

	/**
	 * 非空
	 * 
	 * @param key
	 */
	public FilterMap isNotNull(String key) {
		this.put(key + SPLIT + CriteriaConstant.ISNOTNULL, null);
		return this;
	}

	/**
	 * 空白
	 * 
	 * @param key
	 */
	public FilterMap isEmpty(String key) {
		this.put(key + SPLIT + CriteriaConstant.ISEMPTY, null);
		return this;
	}

	/**
	 * 非空并非空白
	 * 
	 * @param key
	 */
	public FilterMap isNotEmpty(String key) {
		this.put(key + SPLIT + CriteriaConstant.ISNOTEMPTY, null);
		return this;
	}

	/**
	 * sql
	 * 
	 * @param sql
	 */
	public FilterMap sql(String sql) {
		this.put(SPLIT + CriteriaConstant.SQL+SPLIT+new Random().nextInt(1000), sql);
		return this;
	}
	
	
	
	
	/**
	 * sql 示例 filterMap.sqlParams("name like ? ",new Object[]{name},new Type[]{StandardBasicTypes.STRING});
	 * 
	 * @param sql
	 */
	public FilterMap sqlParams(String sql, Object[] value,Type[] type) {
		Object[] das={value,type};
		this.put(SPLIT + CriteriaConstant.SQLPARAMS+SPLIT+new Random().nextInt(1000)+SPLIT+sql,das);
		return this;
	}
	
	

	 

}
